﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Stu_sys.Entity;
using Stu_sys.Models;
using Stu_sys.utils;
using System.Data;
using System.Text.Json;

namespace Stu_sys.Controllers
{
    public class ClassEntityController : Controller
    {
        private readonly MysqlContext _mysqlContext;
        public ClassEntityController(MysqlContext mysqlContext)
        { 
            _mysqlContext=mysqlContext;
        }
        public IActionResult ClassList()
        {
            return View();
        }

        private List<ClassViewModel> RendClassViewModel(List<ClassEntity> list)
        {
            var classViewModels = new List<ClassViewModel>();
            foreach (var item in list)
            {
                var cla = new ClassViewModel
                {
                    ClassEntityId = item.ClassEntityId,
                    ClassName = item.ClassName,
                    GradeId = item.GradeId,
                    GradeName = item.Grade.GradeName,
                    DepartmentId = item.DepartmentId,
                    DepartmentName = item.Department.DepName,
                    ClassRoomId = item.ClassRoomId,
                    ClassRoomName = item.ClassRoom.RoomName,
                    Head = item.Head,
                    Learn = item.Learn,
                    Secretary = item.Secretary,
                    Sport = item.Sport,
                    Work = item.Work,
                    Electron = item.Electron

                };
                classViewModels.Add(cla);
            }
            return classViewModels;
        }

        public IActionResult GetClassList() 
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(ClassViewModel));
            var gradeList = _mysqlContext.ClassEntities
                .Include(c=>c.Department)
                .Include(c=>c.ClassRoom)
                .Include(c=>c.Grade).ToList();
            var classViewModels= RendClassViewModel(gradeList);
            var classStr = JsonSerializer.Serialize(classViewModels);
            var data = new
            {
                attr = attrStr,
                row = classStr
            };
            return Json((data));
        }


        [HttpPost]
        public IActionResult AddClassEntity(ClassEntity classEntity)
        {
            if (IsExistClassName(classEntity)) {
                return Json(-1);
            }
            if (classEntity.GradeId <= 0 || classEntity.DepartmentId <= 0||classEntity.ClassRoomId<=0)
            {
                return Json(0);
            }
            if (classEntity == null)
            {
                return Json(-1);
            }
            _mysqlContext.ClassEntities.Add(classEntity);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult EditClassEntity(ClassEntity classEntity)
        {

            if (classEntity.ClassName == null || classEntity.ClassRoomId < 0)
            {
                return Json(-1);
            }

            var cr = _mysqlContext.ClassEntities.Where(d => d.ClassEntityId == classEntity.ClassEntityId).FirstOrDefault();
            cr.ClassName = classEntity.ClassName;
            cr.ClassTeacher = classEntity.ClassTeacher;
            cr.ClassRoomId = classEntity.ClassRoomId;
            cr.Head = classEntity.Head;
            cr.Learn = classEntity.Learn;
            cr.Secretary = classEntity.Secretary;
            cr.Sport = classEntity.Sport;
            cr.Work = classEntity.Work;
            cr.Electron = classEntity.Electron;

            _mysqlContext.ClassEntities.Update(cr);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult DelClassEntity(ClassEntity classEntity)
        {
            if (classEntity == null)
            {
                return Json(-1);
            }
            var d = _mysqlContext.ClassEntities.Single(d => d.ClassEntityId == classEntity.ClassEntityId);
            _mysqlContext.Remove(d);
            var res = _mysqlContext.SaveChanges();
            return Json(res);
        }

        [HttpPost]
        public IActionResult GetClassByGradeId(ClassEntity classEntity)
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(ClassViewModel));
            var res= _mysqlContext.ClassEntities.Where(c => c.GradeId == classEntity.GradeId)
                .Include(c => c.Department)
                .Include(c => c.ClassRoom)
                .Include(c => c.Grade)
                .ToList();

            var vmlist = RendClassViewModel(res);
            var jsonData=JsonSerializer.Serialize(vmlist);
            var data = new
            {
                attr = attrStr,
                row = jsonData
            };
            return Json(data);
        }

        [HttpPost]
        public IActionResult GetClassByDepartmentId(ClassEntity classEntity)
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(ClassViewModel));
            var res = _mysqlContext.ClassEntities.Where(c => c.DepartmentId == classEntity.DepartmentId)
                .Include(c => c.Department)
                .Include(c => c.ClassRoom)
                .Include(c => c.Grade)
                .ToList();

            var vmlist = RendClassViewModel(res);
            var jsonData = JsonSerializer.Serialize(vmlist);
            var data = new
            {
                attr = attrStr,
                row = jsonData
            };
            return Json(data);
        }



        [HttpPost]
        public IActionResult GetClassByGradeIdAndDepartmentId(ClassEntity classEntity)
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(ClassViewModel));
            var res = _mysqlContext.ClassEntities.Where(c => c.DepartmentId == classEntity.DepartmentId&&c.GradeId==classEntity.GradeId)
                .Include(c => c.Department)
                .Include(c => c.ClassRoom)
                .Include(c => c.Grade)
                .ToList();

            var vmlist = RendClassViewModel(res);
            var jsonData = JsonSerializer.Serialize(vmlist);
            var data = new
            {
                attr = attrStr,
                row = jsonData
            };
            return Json(data);
        }


        public IActionResult SearchClassEntity(ClassEntity classEntity)
        {
            var attrStr = MyUtils.GetAttributeToJsonStr(typeof(ClassViewModel));
            var classList=_mysqlContext.ClassEntities.Where(c => c.ClassName.Contains(classEntity.ClassName))
                .Include(c => c.Department)
                .Include(c => c.ClassRoom)
                .Include(c => c.Grade)
                .ToList();

            var vmlist = RendClassViewModel(classList);
            var jsonData = JsonSerializer.Serialize(vmlist);
            var data = new
            {
                attr = attrStr,
                row = jsonData
            };
            return Json(data);
        }



        /// <summary>
        /// 班级名称是否已存在
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        private bool IsExistClassName(ClassEntity classEntity)
        {
            var cr = _mysqlContext.ClassEntities.Where(o => o.ClassName == classEntity.ClassName).ToList();
            if (cr.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
